package com.example.zkweixiu.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zkweixiu.app.MainActivity;
import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.bean.UserBean;
import com.example.zkweixiu.app.pro.PublicPro;
import com.example.zkweixiu.app.utils.SPUtils;
import com.google.gson.Gson;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxRegTool;
import com.vondear.rxtool.view.RxToast;
import com.yanzhenjie.sofia.Sofia;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class LoginActivity extends AppCompatActivity {

    private TextView tv_yzm_log2;
    private TextView tv_yzm_log;
    private TextView tv_getdt;
    private View tv_getdt2;
    private EditText edit_query;

    private boolean isyzm = false;
    private RelativeLayout rl_log;
    private EditText ed_phone;

    private Context context;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    Toast.makeText(context, "发送成功", Toast.LENGTH_SHORT).show();
                    CountDownTimer timer = new CountDownTimer(60000, 10) {
                        public void onTick(long millisUntilFinished) {
                            tv_getdt.setClickable(false);
                            tv_getdt.setText(millisUntilFinished / 1000 + "秒");
                        }

                        public void onFinish() {
                            tv_getdt.setText("重新发送");
                            tv_getdt.setClickable(true);
                        }
                    };

                    timer.start();
                    break;

                case 205:
                    //Intent intent = new Intent(context,MainActivity.class);

                    RxToast.success(context, "登录成功!", Toast.LENGTH_SHORT, true).show();

                    RxActivityTool.skipActivityAndFinishAll(context, MainActivity.class);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Sofia.with(LoginActivity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));

        context = LoginActivity.this;
        initView();

        isyzm = false;
        tv_getdt.setVisibility(View.GONE);
        tv_getdt2.setVisibility(View.GONE);

        edit_query.setHint("请输入密码");
        tv_yzm_log2.setTextColor(Color.parseColor("#4070ff"));
        tv_yzm_log.setTextColor(Color.parseColor("#afb4c2"));
        ViewPropertyAnimator.animate(tv_yzm_log2).scaleX(1.1f).setDuration(300).setInterpolator
                (new OvershootInterpolator()).start();
        ViewPropertyAnimator.animate(tv_yzm_log2).scaleY(1.1f).setDuration(300).setInterpolator
                (new OvershootInterpolator()).start();

        ViewPropertyAnimator.animate(tv_yzm_log).scaleX(1.0f).setDuration(300).setInterpolator
                (new OvershootInterpolator()).start();
        ViewPropertyAnimator.animate(tv_yzm_log).scaleY(1.0f).setDuration(300).setInterpolator
                (new OvershootInterpolator()).start();
/*        Intent intent = new Intent(this, MainActivity.class);

        startActivity(intent);*/
    }

    private void initView() {
        tv_yzm_log = findViewById(R.id.tv_yzm_log);
        tv_yzm_log2 = findViewById(R.id.tv_yzm_log2);
        tv_getdt = findViewById(R.id.tv_getdt);
        tv_getdt2 = findViewById(R.id.tv_getdt2);
        //手机号
        ed_phone = findViewById(R.id.ed_phone);
        //验证码或者密码
        edit_query = findViewById(R.id.edit_query);

        rl_log = findViewById(R.id.rl_log); //登录

        tv_getdt = findViewById(R.id.tv_getdt); // 获取验证码

        rl_log.setOnClickListener(new View.OnClickListener() { //登录
            @Override
            public void onClick(View view) {
                String phone2 = ed_phone.getText().toString().trim();
                String query = edit_query.getText().toString().trim();

                if (TextUtils.isEmpty(phone2)) {
                    Toast.makeText(context, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!RxRegTool.isMobile(phone2)) {
                    Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }


                if (TextUtils.isEmpty(query)) {
                    Toast.makeText(context, "请输入密码/验证码", Toast.LENGTH_SHORT).show();
                    return;
                }


                if (isyzm) {
                    loginWeiXiu(phone2,query,"2");
                }else {
                    loginWeiXiu(phone2,query,"1");
                }
            }
        });

        tv_getdt.setOnClickListener(new View.OnClickListener() { //获取验证码
            @Override
            public void onClick(View view) {

                String phone2 = ed_phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone2)) {
                    Toast.makeText(context, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!RxRegTool.isMobile(phone2)) {
                    Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                sendSIM(phone2);
            }
        });


        tv_yzm_log2.setOnClickListener(new View.OnClickListener() {  //密码登录
            @Override
            public void onClick(View view) {
                tv_yzm_log2.setTextColor(Color.parseColor("#4070ff"));
                tv_yzm_log.setTextColor(Color.parseColor("#afb4c2"));
/*                ViewHelper.setScaleX(tv_yzm_log2,1.1f);
                ViewHelper.setScaleY(tv_yzm_log2,1.1f);
                ViewHelper.setScaleY(tv_yzm_log,1f);
                ViewHelper.setScaleX(tv_yzm_log,1f);*/

                isyzm = false;
                tv_getdt.setVisibility(View.GONE);
                tv_getdt2.setVisibility(View.GONE);

                edit_query.setHint("请输入密码");

                ViewPropertyAnimator.animate(tv_yzm_log2).scaleX(1.1f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();
                ViewPropertyAnimator.animate(tv_yzm_log2).scaleY(1.1f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();

                ViewPropertyAnimator.animate(tv_yzm_log).scaleX(1.0f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();
                ViewPropertyAnimator.animate(tv_yzm_log).scaleY(1.0f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();

            }
        });


        tv_yzm_log.setOnClickListener(new View.OnClickListener() {  //验证码登录
            @Override
            public void onClick(View view) {

                isyzm = true;
                tv_getdt.setVisibility(View.VISIBLE);
                tv_getdt2.setVisibility(View.VISIBLE);
                tv_yzm_log.setTextColor(Color.parseColor("#4070ff"));
                tv_yzm_log2.setTextColor(Color.parseColor("#afb4c2"));
                edit_query.setHint("请输入验证码");
                ViewPropertyAnimator.animate(tv_yzm_log).scaleX(1.1f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();
                ViewPropertyAnimator.animate(tv_yzm_log).scaleY(1.1f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();

                ViewPropertyAnimator.animate(tv_yzm_log2).scaleX(1.0f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();
                ViewPropertyAnimator.animate(tv_yzm_log2).scaleY(1.0f).setDuration(300).setInterpolator
                        (new OvershootInterpolator()).start();
/*                ViewHelper.setScaleX(tv_yzm_log2,1f);
                ViewHelper.setScaleY(tv_yzm_log2,1f);
                ViewHelper.setScaleY(tv_yzm_log,1.1f);
                ViewHelper.setScaleX(tv_yzm_log,1.1f);*/
            }
        });
    }

    /**
     * 维修端登录
     *
     * @param phone 手机号
     * @param par   参数
     * @param types 登录方式
     */
    private void loginWeiXiu(String phone, String par, String types) {
        OkHttpUtils
                .get()
                .url(PublicPro.LOGIN_WX)
                .addParams("phone", phone)
                .addParams("par", par)
                .addParams("types", types)
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);


                        UserBean userBean = new Gson().fromJson(response, UserBean.class);

                        if (userBean.getStatus() == 200) {
                           // handler.sendEmptyMessage(200);

                            PublicPro.userBean = userBean;
                            SPUtils.put(context, "user", response);

                           // SPUtils.clear(context);
                            //PublicPro.userBean = null;
                           // RxActivityTool.skipActivityAndFinishAll(getActivity(), LoginActivity.class);
                            handler.sendEmptyMessage(205);
                        } else {
                            Toast.makeText(context, userBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }

    private void sendSIM(String phone2) {
        OkHttpUtils
                .get()
                .url(PublicPro.PUT_SIM)
                .addParams("phone", phone2)
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);


                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);

                        if (objBean.getStatus() == 200) {
                            handler.sendEmptyMessage(200);
                        } else {
                            Toast.makeText(context, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }
}
